import React from 'react';
import { KFind, ck_empty, Info, KDateTime, ck_date, obj_int, obj_str, loc, ck_int, useKForm, KDlg, KInput } from 'sui';
import { kind_name } from './cmd';

const DlgNum = (props) => {
    const form = useKForm(props.data);

    const ck = {
        num : v => {
            const e = ck_int(v);
            if (e) {
                return e;
            }

            const tmp = parseInt(v, 10);
            const pnum = obj_int(props.data, "pnum");
            const num = obj_int(props.data, "num");
            const count = pnum - num;

            if (props.fin === "back") {
                if (tmp > num) {
                    return loc("l_707");
                }
            } else {
                if (tmp > count) {
                    return loc("l_707");
                }
            }

            return null;
        },
    };

    const get_kind = () => {
        const name = kind_name();
        const k = obj_str(props.data, "kind");
        return obj_str(name, k);
    };

    const get_col = () => {
        return [
            {sn: "sn", name: loc("l_518")},
            {sn: "kind", name: loc("l_229")},
            {sn: "pnum", name: loc("l_146")},
            {sn: "num", name: loc("l_473")},
            {sn: "dnum", name: loc("l_567")},
        ];
    };

    const get_data = () => {
        const pnum = obj_int(props.data, "pnum");
        const num = obj_int(props.data, "num");

        return {
            sn: obj_str(props.data, "sn"),
            kind: get_kind(),
            pnum: pnum,
            num: num,
            dnum: pnum - num,
        };
    };

    const getTitle = () => {
        if (props.fin === "back") {
            return loc("l_767");
        }

        return loc("l_566");
    };

    const getH = () => {
        if (props.fin === "back") {
            return ["num"];
        }

        return ["dnum"];
    };

    return (
        <KDlg {...props} title={getTitle()} ck={ck} form={form}>
            <Info data={get_data()} col={get_col()} hl={getH()}/>
            <KInput label={loc("l_127")} form={form} name="num"/>
        </KDlg>
    );
};

const DlgTime = (props) => {
    const form = useKForm(props.data);

    const ck = {
        pend : v => ck_date(v),
        supply : v => ck_empty(v),
    };

    const get_kind = () => {
        const name = kind_name();
        const k = obj_str(props.data, "kind");
        return obj_str(name, k);
    };

    const get_col = () => {
        return [
            {sn: "sn", name: loc("l_518")},
            {sn: "kind", name: loc("l_229")},
            {sn: "std", name: loc("l_547")},
            {sn: "pnum", name: loc("l_146")},
        ];
    };

    const get_data = () => {
        return {
            sn: obj_str(props.data, "sn"),
            kind: get_kind(),
            std: obj_str(props.data, "std"),
            pnum: obj_str(props.data, "pnum"),
        };
    };

    const getSupply = () => {
        const kind = obj_str(props.data, "kind");
        const map_supply = props.map_supply;
        const supply = props.supply;

        if (!map_supply || !kind || !supply) {
            return null;
        }

        const f = supply.find(d => d.sn === kind);
        if (!f) {
            return null;
        }

        if (!f.user) {
            return null;
        }

        const tmp = [];

        f.user.forEach(t => {
            const v = map_supply[t];
            if (!v) {
                return;
            }

            tmp.push({ sn: t, name: v });
        });

        return tmp;
    };

    return (
        <KDlg {...props} title={loc("l_563")} ck={ck} form={form}>
            <Info data={get_data()} col={get_col()}/>
            <KFind label={loc("l_739")} name="supply" form={form} list={getSupply()} className="mt-2"/>
            <KDateTime label={loc("l_544")} form={form} name="pend"/>
        </KDlg>
    );
};

export {DlgNum, DlgTime};
